﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataItems : xIx.Web.UI.PageBase
{
    public string SelNode
    {
        get
        {
            object v = ViewState["SelNode"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["SelNode"] = value;
            //Label1.Text = value;
        }
    }
    public string SelID
    {
        get
        {
            return TreeView1.SelectedValue;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SelNode = string.Empty;

            TreeView1.Nodes.Clear();
            TreeNode r = new TreeNode();
            if (string.IsNullOrWhiteSpace(Request["ID"]))
            {
                r.Text = "网站"; r.Value = string.Empty;
            }
            else {
                using (var dh=new xIx.Data.DbHelper())
                {
                    DataTable dt= dh.Table["Sys_ListItems"].Where("ID=@ID").Fields("ID,Title").Select(Request["ID"]);
                    r.Text = dt.Rows[0][1].ToString(); // "内容管理";
                    r.Value = Request["ID"];
                }
            }

            TreeView1.Nodes.Add(r);
            r.Select();

            ReloadTree();
        }
    }
    protected void ResetButton_Click(object sender, ImageClickEventArgs e)
    {
        ReloadGrid();
    }
    protected void AddButton_Click(object sender, ImageClickEventArgs e)
    {
        ReloadGrid();
    }
    protected void SaveButton_Click(object sender, ImageClickEventArgs e)
    {
            DataTable dt = null;
            int ec = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                try
                {
                    if (gr.RowType == DataControlRowType.DataRow)
                    {
                        string rid = (gr.Cells[1].FindControl("RID") as Label).Text;
                        if ((gr.Cells[3].FindControl("Del") as CheckBox).Checked)
                        {
                            using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
                            {

                                dh.Table["Sys_ListItems"].Where("ID=@RID").Delete(rid);
                                //----------
                            }
                        }
                        else
                        {
                            if ((gr.Cells[2].FindControl("Save") as CheckBox).Checked)
                            {
                                double vs = 10;
                                double.TryParse((gr.Cells[6].FindControl("SortIdx") as TextBox).Text,out vs);
                                DateTime nd = DateTime.Now;
                                DateTime.TryParse((gr.Cells[8].FindControl("ContentTime") as TextBox).Text, out nd);

                                using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
                                {
                                    int erc = dh.Table["Sys_ListItems"]
                                        .Fields("Title,SortIdx,Img,ContentTime,Editor,UpdateTime,IsAudit".Split(','))
                                        .UpdateWith(
                                            (gr.Cells[4].FindControl("Title") as TextBox).Text
                                            , vs
                                            , (gr.Cells[5].FindControl("Img") as UserControls_ImgSelector).Value
                                            , nd
                                            , (gr.Cells[7].FindControl("Editor") as TextBox).Text
                                            , DateTime.Now
                                            , (gr.Cells[9].FindControl("IsAudit") as CheckBox).Checked && CanAudit
                                            )
                                        .Where("ID=@RID")
                                        .Exec(rid);
                                }

                            }
                        }

                    }
                }
                catch
                {
                    ec++;
                }
            }
            if (ec > 0)
            {
                Alert("保存时遇到错误。");
            }

        ReloadTree();
    }
    public bool IsManaLinkVisible = false;
    protected void ReloadGrid()
    {
        
        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            DataTable tempdt = dh.Table["Sys_Groups"].Fields("IsItemHasSubGroup").Where("ID=@ID").Select(SelID);
            IsManaLinkVisible= (tempdt.Rows.Count>0)&&("1"==tempdt.Rows[0][0].ToString());

            dt = dh.Table["Sys_ListItems"].Where("Gid=@SID").OrderBy("SortIdx Desc,ContentTime Desc").Select(SelID);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

        HyperLink1.DataBind();
    }
    protected void ReloadTree()
    {

        TreeNode r = TreeView1.FindNode(SelNode);
        if (r == null)
        {
            r = TreeView1.Nodes[0];
            SelNode = r.ValuePath;
        }
        r.Select();
        r.ChildNodes.Clear();

        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            dt = dh.Table["Sys_Groups"].Fields("ID", "Title", "Pid", "SortIdx").OrderBy("SortIdx Desc").Select();
        }
        BindTreeSubNodes(r, dt);
        r.ExpandAll();

        ReloadGrid();
    }

    protected void BindTreeSubNodes(TreeNode r, DataTable dt)
    {
        foreach (DataRow dr in dt.Select("Pid='" + r.Value + "'", "SortIdx Desc"))
        {
            TreeNode n = new TreeNode();
            n.Text = dr["Title"].ToString(); n.Value = dr["ID"].ToString();
            r.ChildNodes.Add(n);
            BindTreeSubNodes(n, dt);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ReloadTree();

    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        SelNode = TreeView1.SelectedNode.ValuePath;
        Label3.Text = TreeView1.SelectedNode.Text + "[" + TreeView1.SelectedNode.ValuePath + "]";

        ReloadGrid();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ReloadGrid();
    }
    public bool CanAudit
    {
        get { return (User.AccountInfo.Role == "Manager" || User.AccountInfo.Role == "Admin"); }
    }
}
